//
//  DragView.m
//  SimpleSearch
//
//  Created by Ryan on Thu Oct 30 2003.
//
//  ****************
//  Short Disclaimer (please read!):
//
//  Why open source?  This project was briefly popular several years ago and has since lost interest (on both
//  my part and the public's part).  Rather than letting it moulder for eternity, I decided to open source it 
//  in case anyone else is interested in renovating it and bringing it up to date.
//
//  My main concern with open sourcing it is that this program is one of the first Cocoa programs I wrote and
//  as such, it is in no way representative of my current coding style!  Many things are done incorrectly in 
//  this code base but I have not taken the time to revise them for the open source release. Hence, if you work
//  for a company looking to hire me, don't look too critically at this old code!
//
//  This code was originally written in 2002 to 2003 and hence was created before technologies such as 
//  Cocoa Bindings and properties (Obj-C 2.0) existed.  Much of the code could be ripped out and replaced 
//  with the newer way of doing things.
//
//  The GUI is also fairly out of date and in need of some serious updating.
//  
//  Have fun!
//  April, 2008.
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.


#import "DragView.h"


@implementation DragView

- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
		visible = YES;
    }
    return self;
}

- (void)drawRect:(NSRect)rect {
	if (!visible) return;  // Only draw if it's supposed to be visible.
	
    // Drawing code here.
    NSRect bounds = [self bounds];

	float gray = 223.0/255.0;
	[[NSColor colorWithCalibratedRed:gray green:gray blue:gray alpha:1.0] set];
    [NSBezierPath fillRect:bounds];

    [[NSColor darkGrayColor] set];
    NSBezierPath * p = [NSBezierPath bezierPath];
    NSPoint top, bottom;

    top.x = 3;
    bottom.x = 3;
    top.y = bounds.size.height - 3;
    bottom.y = 3;

    [p setLineWidth: 0.5];

    [p moveToPoint: top];
    [p lineToPoint: bottom];

    top.x += 3;
    bottom.x +=3;

    [p moveToPoint: top];
    [p lineToPoint: bottom];

    [p stroke];

//  [[self window] setHasShadow: NO];
}


- (void)setIsOnLeft:(BOOL)b {
	onLeft = b;
}

- (BOOL)isOnLeft { return onLeft; }

/*
- (void)expandWindowOutOfView {
	NSRect newFrame;

	newFrame.origin = [self frame].origin;			
	newFrame.origin = [[self window] convertBaseToScreen: newFrame.origin];
	
	if (! [self isOnLeft]) {
		// If it's on the right hand side...
		newFrame.origin = [self frame].origin;
		newFrame.origin.x -= originalWindowFrame.size.width;
		newFrame.origin.x += [self frame].size.width;
	}

	newFrame.size.width = originalWindowFrame.size.width;
	newFrame.size.height = originalWindowFrame.size.height;

	isCollapsed = NO;	
	[[self window] setFrame: newFrame display:YES animate:NO];	
}


// Collapses the search window so it's the same size and position as this view.
- (void)collapseWindowIntoView {
	NSRect newFrame;

	originalWindowFrame = [[self window] frame];
	
	newFrame.origin = [self frame].origin;
	
	// Convert this into screen coordinates.
	newFrame.origin = [[self window] convertBaseToScreen: newFrame.origin];
	//[[self window] frame].origin;
	
	newFrame.size.width = [self frame].size.width;
	newFrame.size.height = [self frame].size.height;

	isCollapsed = YES;	
	[[self window] setFrame: newFrame display:YES animate:NO];
}
*/


- (void)mouseUp:(NSEvent *)theEvent {

	// Let the controller know that the user released the mouse...
	NSNotificationCenter *nc;
	nc = [NSNotificationCenter defaultCenter];
	[nc postNotificationName:@"dragViewMouseUp" object:self];	
}

- (void)mouseDown:(NSEvent *)theEvent {
	mouseDidJustDrag = NO;

    NSRect  windowFrame = [[self window] frame];

	// Where is the click in the window coordinates?
	initialLocation = [theEvent locationInWindow];

	// Convert it to screen coordinates
	initialLocation = [[self window] convertBaseToScreen: initialLocation];

	initialLocation.x -= windowFrame.origin.x;
	initialLocation.y -= windowFrame.origin.y;

	
	// Let the controller know that the user clicked the mouse...
	NSNotificationCenter *nc;
	nc = [NSNotificationCenter defaultCenter];
	[nc postNotificationName:@"dragViewMouseDown" object:self];

}


- (BOOL)isOpaque { return YES; }

- (BOOL)didMouseJustDrag { return mouseDidJustDrag; }


// We have to handle moving the window ourself since the window doesn't have a title bar.  Fun stuff! 
- (void)mouseDragged:(NSEvent *)theEvent {
	mouseDidJustDrag = YES;

	NSPoint currentLocation;
	NSPoint newOrigin;
	NSRect  windowFrame = [[self window] frame];

	currentLocation = [[self window] convertBaseToScreen: [theEvent locationInWindow]];
//	NSLog(@"currentLocation: (%f, %f)", currentLocation.x, currentLocation.y);

    newOrigin.x = currentLocation.x - initialLocation.x;
    newOrigin.y = currentLocation.y - initialLocation.y;

	NSRect visibleScreen = [[NSScreen mainScreen] visibleFrame];

	// Prevent it from going above the menu bar, below the dock, etc...
	if (newOrigin.y < visibleScreen.origin.y) {
		newOrigin.y = visibleScreen.origin.y;
	}
	if ((newOrigin.y + windowFrame.size.height) >
	 (visibleScreen.origin.y + visibleScreen.size.height )) {
		newOrigin.y = visibleScreen.origin.y + visibleScreen.size.height - 
		windowFrame.size.height;
	}	
	if (newOrigin.x < visibleScreen.origin.x) {
		newOrigin.x = visibleScreen.origin.x;
	}
	if ( (newOrigin.x + windowFrame.size.width) > 
	   (visibleScreen.origin.x + visibleScreen.size.width)) {
		newOrigin.x = visibleScreen.origin.x + visibleScreen.size.width - 
		windowFrame.size.width;
	}
		

    // Move the window to the new location.
    [[self window] setFrameOrigin:newOrigin];
}

- (BOOL)isVisible { return visible; }

- (void)setVisible:(BOOL)b {
	visible = b;
	[self setNeedsDisplay:YES];
}

- (void)moveUp {
	NSRect r = [self frame];
	r.origin.y += 500;

	[self setFrame: r];
	[self setNeedsDisplay: YES];
}

- (void)moveDown {
	NSRect r = [self frame];
	r.origin.y -= 500;
	[self setFrame: r];
	[self setNeedsDisplay: YES];	
}


// This allows the user to click once and move the window without
// first having to activate it and bring it to the front.
- (BOOL)acceptsFirstMouse:(NSEvent *)theEvent {
	return YES;
}

@end
